﻿@using PagedList;
@using PagedList.Mvc;
@model IEnumerable<StartMVC.Models.Product>
<link href="~/Content/PagedList.css" rel="stylesheet" />
@{
    var ajaxOption = new AjaxOptions()
    {
        OnSuccess = "AddToCartSuccess",
        OnFailure = "AddToCartFailure",
        HttpMethod = "Post"
    };
}
@section scripts{
    @Scripts.Render("~/bundles/jqueryval")
    <script>
        function AddToCartSuccess() {
            alert("添加购物车成功");
        }
        function AddToCartFailure(xhr) {
            alert("添加购物车失败(HTTP状态码: " + xhr.status + ")");
        }
    </script>
}
<h2>@Html.DisplayNameFor(model => model.ToList()[0])</h2>
<h3>您正在浏览[@Model.First().ProductCategory.Name]分类的商品:</h3>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
        <th>
            添加购物车
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.ActionLink(item.Name, "ProductDetail", new { id = item.Id })
            </td>
            <td>
                @Html.DisplayFor(modelitem => item.Description)
            </td>
            <td>
                @Html.DisplayFor(modelitem => item.Price)
            </td>
            <td>
                @Ajax.ActionLink("添加购物车", "AddToCart", "Cart", new { ProductId = item.Id }, ajaxOption)
            </td>
        </tr>

    }
  
</table>

<div>
    @{
        var data = Model as IPagedList<StartMVC.Models.Product>;
    }

    @Html.PagedListPager(list: data, generatePageUrl: Page => Url.Action("ProductList", new { p = Page }))
</div>